<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   站内短信
 */
defined('IN_YuLin') || exit('NO PERMIT!');

UserLogin(U($m.'/'.$c.'/'.$a));

$table = Table('user_message');
$stable = Table('user_message_send');
$mtable = Table('user');
$rtable = Table('user_recommend');

$head['title'] = '站内短信';

if(IS_POST)
{
	if($a == 'send')
	{
		$title		=	NoBadStr($_POST['title']);
		$content	=	NoBadStr($_POST['content']);
		
		!$title && AjaxReturn(1,'请输入信息标题');
		!$content && AjaxReturn(1,'请输入信息内容');
		
		$tid = $_POST['tid'] ? 1 : 0;	//	0上级1下级
		if($tid)
		{
			$uids = NoBadStr($_POST['uids']);
			!$uids && AjaxReturn(1,'请选择接受信息的会员');
			
			$uids = explode(',',$uids);
			foreach($uids as $key => $val)
			{
				$uids[$key] = intval($val);
			}
			$uids = array_unique($uids);
		}
		else
		{
			$tmp = $db->getrow('SELECT ruid FROM '.$rtable.' WHERE uid = '.$_uid);
			$uids = [intval($tmp['ruid'])];
		}
		
		//	发件箱
		$ins = [];
		$ins['tid']		=	$tid;
		$ins['uid']		=	$_uid;
		$ins['tuids']	=	implode(',',$uids);
		$ins['title']	=	$title;
		$ins['content']	=	$content;
		$ins['dateline']=	TIMESTAMP;
		$db->add('user_message_send',$ins);
		
		//	收件箱
		foreach($uids as $val)
		{
			if($val > 0)
			{
				$ins = [];
				$ins['uid']		=	$val;
				$ins['fuid']	=	$_uid;
				$ins['title']	=	$title;
				$ins['content']	=	$content;
				$ins['status']	=	0;
				$ins['dateline']=	TIMESTAMP;
				$db->add('user_message',$ins);
			}
		}
		
		AjaxReturn(0,'发送站内消息成功');
	}
}

if($a == 'send')	//	发件箱
{
	$data  = [];
	$where = ' FROM '.$stable.' t LEFT JOIN '.$mtable.' m ON t.tuids = m.id WHERE t.uid = '.$_uid.' AND t.status != 2';

	$page = max(1,intval($_GET['page']));
	$size = max(1,intval($_GET['size']));

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$limit = ' LIMIT '.($page - 1) * $size.','.$size;
		
		$data = $db->getall('SELECT t.*,m.username '.$where.$orderby.$limit);
		foreach($data as $key => $val)
		{
			$val['tid'] = $val['tid'] ? '下级' : '上级';
			$val['dateline'] = date('Y/m/d H:i:s',$val['dateline']);
			
			$data[$key] = $val;
		}
	}
	
	echo json_encode(['pageIndex'=>$page,'pageSize'=>$size,'total'=>intval($tmp['p']),'rows'=>$data]);
	exit;
}
else if($a == 'senddelete')	//	发件箱删除
{
	$ids = NoBadStr($_GET['ids']);
	!$ids && AjaxReturn(1,'请选择要删除的信息');
	
	$ids = explode(',',$ids);
	foreach($ids as $key => $val)
	{
		$ids[$key] = intval($val);
	}
	
	$db->exec('UPDATE '.$stable.' SET status = 2 WHERE id IN ('.implode(',',$ids).') AND uid = '.$_uid.' AND status != 2');	//	删除
	
	AjaxReturn(0,'删除成功');
}
else if($a == 'receive')	//	收件箱
{
	$data  = [];
	$where = ' FROM '.$table.' t LEFT JOIN '.$mtable.' m ON t.fuid = m.id WHERE t.uid = '.$_uid.' AND t.status != 2';

	$page = max(1,intval($_GET['page']));
	$size = max(1,intval($_GET['size']));

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$limit = ' LIMIT '.($page - 1) * $size.','.$size;
		
		$data = $db->getall('SELECT t.*,m.username '.$where.$orderby.$limit);
		foreach($data as $key => $val)
		{
			$val['dateline'] = date('Y/m/d H:i:s',$val['dateline']);
			
			$data[$key] = $val;
		}
	}
	
	echo json_encode(['pageIndex'=>$page,'pageSize'=>$size,'total'=>intval($tmp['p']),'rows'=>$data]);
	exit;
}
else if($a == 'receiveupdate')	//	收件箱更新状态
{
	$id = intval($_GET['id']);
	$id && $db->exec('UPDATE '.$table.' SET status = 1 WHERE id = '.$id.' AND uid = '.$_uid.' AND status = 0');	//	已读
	
	exit;
}
else if($a == 'receivedelete')	//	收件箱删除
{
	$ids = NoBadStr($_GET['ids']);
	!$ids && AjaxReturn(1,'请选择要删除的信息');
	
	$ids = explode(',',$ids);
	foreach($ids as $key => $val)
	{
		$ids[$key] = intval($val);
	}
	
	$db->exec('UPDATE '.$table.' SET status = 2 WHERE id IN ('.implode(',',$ids).') AND uid = '.$_uid.' AND status != 2');	//	删除
	
	AjaxReturn(0,'删除成功');
}
else if($a == 'downuser')	//	我的下级
{
	$data = $db->getall('SELECT t.uid,m.username FROM '.$rtable.' t LEFT JOIN '.$mtable.' m ON t.uid = m.id WHERE t.ruid = '.$_uid.' AND t.status = 1');
	
	echo json_encode($data);
	exit;
}

$tpl->display('member/message');